package com.zhuss.opus.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.opus.opus.wechat.DelTopicOpusBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.opus.db.dao.ext.OpusTopicDao;
import com.zhuss.util.JsonUtil;

/**
 * 删除评论作品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("opusDelTopic")
public class OpusDelTopic extends AbstractProcessor<Object, DelTopicOpusBo> implements Processor<Object, DelTopicOpusBo> {

    @Autowired
	@Qualifier("opusRead")
	private OpusRead opusRead;
    
    @Autowired
	@Qualifier("opusTopicRead")
	private OpusTopicRead opusTopicRead;
    
    @Autowired
    private OpusTopicDao opusTopicDao;
    
    /**
     * 校验
     */
    @Override
    public void check(DelTopicOpusBo bo) throws WpsException {
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(DelTopicOpusBo param) throws WpsException {
    	Long opusTopicId = param.getOpusTopicId();
    	List<Long> opusTopicIdList = param.getOpusTopicIdList();
    	opusTopicIdList = new ArrayList<Long>();
    	opusTopicIdList.add(opusTopicId);
    	param.setOpusTopicIdList(opusTopicIdList);
    	batchProcess(param);
    	param.setName(opusTopicId + "");
    	return null;
    }
    
    private void batchProcess(DelTopicOpusBo param) throws WpsException {
    	List<Long> opusTopicIdList = param.getOpusTopicIdList();
    	
    	if (opusTopicIdList == null || opusTopicIdList.isEmpty()) {
    		return;
    	}
    	
    	Integer deleted = param.getDeleted();
    	Long modifierId = param.getConcurrentUserId();
    	String modifier = param.getConcurrentUserName();
    	Date modifyTime = new Date();
    	
    	List<Long> opusTopicSonList = opusTopicRead.listSon(opusTopicIdList);
    	DelTopicOpusBo sonBo = new DelTopicOpusBo();
    	BeanUtils.copyProperties(param, sonBo);
    	if (opusTopicSonList != null && !opusTopicSonList.isEmpty()) {
    		sonBo.setOpusTopicIdList(opusTopicSonList);
    	} else {
    		sonBo.setOpusTopicIdList(null);
    	}
    	batchProcess(sonBo);
    	opusTopicDao.deleteByParentBatch(opusTopicIdList, deleted, modifierId, modifier, modifyTime);
    	opusTopicDao.deleteBatch(opusTopicIdList, deleted, modifierId, modifier, modifyTime);
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DelTopicOpusBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.作品.key());
    	record.setAction(LogActionEnum.作品删除评论.key());
    	record.setTargetId(param.getOpusTopicId());
    	record.setTargetName("评论内容：" + param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
